<template>
  <div class="home-box">
    <header class="home-header" :class="{ active: isScroll }">
      <div class="left-header">
        <router-link tag="i" to="./category"><i class="iconfont icon-tabulation"></i></router-link>
      </div>
      <div class="header-search">
        <span class="app-name">山河无恙</span>
        <i class="iconfont icon-search"></i>
        <router-link tag="span" class="search-title" to="./product-list?from=home"
          >人间皆安</router-link
        >
      </div>
      <router-link class="iconfont icon-mine" tag="span" to="./login" v-if="!isLogin"
        >登录</router-link
      >
      <router-link class="iconfont icon-mine" tag="span" to="./user" v-else> </router-link>
      <!-- <span class="iconfont icon-tabulation"></span> -->

      <!-- <div class="middle-header">
        <van-search v-model="searchValue" placeholder="请输入搜索关键词" />
      </div>
      <div class="right-header">
        <span class="iconfont icon-mine"></span>
      </div> -->
    </header>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" lazy-render>
      <van-swipe-item v-for="image in carouselsList">
        <img :src="image.carouselUrl" alt="" />
      </van-swipe-item>
    </van-swipe>
    <van-grid :column-num="5" :border="false">
      <van-grid-item v-for="(item, index) in GridList">
        <template #icon>
          <span class="iconfont" :class="item.iconName"></span>
        </template>
        <template #text>
          <span class="text">{{ item.title }}</span>
        </template>
      </van-grid-item>
    </van-grid>
    <HomePanel :GoodList="newGoodsesList">
      <template v-slot:title>
        <div><h3>新品上线</h3></div>
      </template>
    </HomePanel>
    <HomePanel :GoodList="hotGoodsesList">
      <template v-slot:title>
        <div><h3>热销产品</h3></div>
      </template>
    </HomePanel>
    <HomePanel :GoodList="recommendGoodsesList">
      <template v-slot:title>
        <div><h3>精选推荐</h3></div>
      </template>
    </HomePanel>
  </div>
</template>

<script setup>
  import { getHomeMultidata } from "@/api/home"
  import { ref, reactive, onMounted, onBeforeUnmount } from "vue"
  import HomePanel from "@/components/HomePanel.vue"
  import { showLoadingToast } from "vant"
  let isScroll = ref(false)
  let isLogin = ref(false)
  let searchValue = ref("")
  let carouselsList = reactive([])
  let hotGoodsesList = reactive([])
  let newGoodsesList = reactive([])
  let recommendGoodsesList = reactive([])
  let GridList = reactive([
    { title: "小黑盒", iconName: "icon-zuanshi" },
    { title: "百亿补贴", iconName: "icon-chaozhijifen" },
    { title: "全球购", iconName: "icon-jisutuikuan" },
    { title: "超市到家", iconName: "icon-gouwucheman" },
    { title: "充值缴费", iconName: "icon-chongzhi" },
    { title: "淘鲜达", iconName: "icon-huodongyouxian" },
    { title: "潮流服饰", iconName: "icon-Txu" },
    { title: "领券中心", iconName: "icon-tianmaohaoquan" },
    { title: "省钱", iconName: "icon-tianmaopaidui" },
    { title: "分类", iconName: "icon-liebiao" },
  ])
  onMounted(() => {
    window.addEventListener("scroll", changeScroll)
    const token = localStorage.getItem("token")
    if (token) {
      isLogin.value = true
    }
    showLoadingToast({
      message: "加载中...",
      forbidClick: true,
    })
    getAllDatas()
  })
  let changeScroll = () => {
    let scrollVal = document.documentElement.scrollTop || document.body.scrollTop
    if (scrollVal > 100) {
      isScroll.value = true
    } else {
      isScroll.value = false
    }
  }
  onBeforeUnmount(() => {
    window.removeEventListener("scroll", changeScroll)
  })
  // 获取首页数据
  const getAllDatas = async () => {
    const {
      data: { carousels, hotGoodses, newGoodses, recommendGoodses },
      resultCode,
    } = await getHomeMultidata()
    if (resultCode === 200) {
      carousels.forEach(item => carouselsList.push(item))
      hotGoodses.forEach(item => hotGoodsesList.push(item))
      newGoodsesList.push(...newGoodses)
      recommendGoodsesList.push(...recommendGoodses)
    }
  }
</script>

<style scoped lang="less">
  @import "@/assets/css/public";
  .home-box {
    padding-bottom: 50px;
    .home-header {
      position: fixed;
      left: 0;
      top: 0;
      .wh(100%, 50px);
      .fj();
      line-height: 50px;
      padding: 0 15px;
      .boxSizing();
      font-size: 15px;
      color: #fff;
      z-index: 10000;
      .nbmenu2 {
        color: @primary;
      }
      &.active {
        background: @primary;
        .nbmenu2 {
          color: #fff;
        }
        .login {
          color: #fff;
        }
      }

      .header-search {
        display: flex;
        width: 74%;
        line-height: 20px;
        margin: 10px 0;
        padding: 5px 0;
        color: #232326;
        background: rgba(255, 255, 255, 0.7);
        border-radius: 20px;
        .app-name {
          padding: 0 10px;
          color: @primary;
          font-size: 20px;
          font-weight: bold;
          border-right: 1px solid #666;
        }
        .icon-search {
          padding: 0 10px;
          font-size: 17px;
        }
        .search-title {
          font-size: 12px;
          color: #666;
          line-height: 21px;
        }
      }
      .icon-iconyonghu {
        color: #fff;
        font-size: 22px;
      }
      .login {
        color: @primary;
        line-height: 52px;
        .van-icon-manager-o {
          font-size: 20px;
          vertical-align: -3px;
        }
      }
    }
    .my-swipe .van-swipe-item {
      img {
        width: 100%;
        height: auto;
      }
    }
    .van-grid {
      .van-grid-item {
        .text {
          font-size: 12px;
        }
        .iconfont {
          font-size: 30px;
          color: #00a6a4;
        }
      }
    }
  }
</style>
